package at.ac.tuwien.prog.command;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import at.ac.tuwien.prog.entity.Register;
import at.ac.tuwien.prog.memory.IMemoryManager;
import at.ac.tuwien.prog.system.ISystem;

/**
 * 
 * @author christoph.pickl@student.tuwien.ac.at
 */
public class AddConstantCommand extends AbstractCommand {
	
	private static final Log LOG = LogFactory.getLog(AddConstantCommand.class);

	public static final String NAME = "addc";

	/**
	 * @param constantValue value to store in register
	 * @param registerAddress target address
	 */
	public AddConstantCommand(int registerAddress, int constantValue) {
		super(registerAddress, constantValue);
	}

	public void execute(final ISystem system) {
		final int registerAddress = this.getOperand1();
		final int constantValue = this.getOperand2();
		
		LOG.debug("execute(registerAddress="+registerAddress+"; constantValue="+constantValue+")");
		
		final IMemoryManager memory = system.getMemoryManager();
		
		final Register oldValue = memory.getRegisterValue(registerAddress);
		final Register newValue = Register.newByInt(oldValue.getIntValue() + constantValue);
		LOG.trace("Adding [" + constantValue + "] to old value: " + oldValue + " => new value: " + newValue);
		// TODO minor: range checks
		
		memory.setRegisterValue(registerAddress, newValue);
	}

}
